{% extends "base.html" %}
{# Copyright The IETF Trust 2015, All Rights Reserved #}
{% load origin %}
{% load django_bootstrap5 %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
    {% origin %}
    <h1>{{ title }}</h1>
    {% if not option %}
        <p class="my-3">
            <a class="btn btn-info"
               href="{% url 'ietf.doc.views_help.state_help' type="charter" %}">Help on states</a>
        </p>
    {% endif %}
    <form method="post">
        {% csrf_token %}
        {% for field in form.visible_fields %}
            {% bootstrap_field field %}
            {% if field.name == "charter_state" and chartering_type == "rechartering" %}
                <div class="my-3 form-check ballot-wo-extern">
                    <input class="form-check-input"
                           type="checkbox"
                           id="ballot_wo_extern"
                           name="ballot_wo_extern">
                    <label class="form-check-label" for="ballot_wo_extern">Ask whether external review can be skipped in ballot.</label>
                </div>
            {% endif %}
        {% endfor %}
        {% if initial_review %}
            <p class="alert alert-warning my-3">
                <b>Warning:</b> Announced initial review time hasn't elapsed yet. It does so at {{ initial_review.expires }}.
            </p>
        {% endif %}
        {% if option == "initcharter" or option == "recharter" %}
            <button class="btn btn-primary" type="submit">Initiate chartering</button>
        {% endif %}
        {% if not option or option == "abandon" %}
            <button class="btn btn-primary" type="submit">Save &amp; notify secretariat</button>
            <a class="btn btn-secondary float-end"
               href="{% url "ietf.doc.views_doc.document_main" name=doc.name %}">Back</a>
        {% endif %}
    </form>
    {% if prev_state %}
        <h3>Or revert to previous state</h3>
        <div class="prev-state">
            <form method="post">
                {% csrf_token %}
                <input type="hidden"
                       name="charter_state"
                       value="{{ prev_charter_state.pk }}">
                <input type="hidden" name="state" value="{{ prev_state.slug }}">
                <input type="submit" value="Back to {{ prev_charter_state.name }}">
            </form>
        </div>
    {% endif %}
{% endblock %}
{% block js %}
    <script>
        var info_msg = {{ info_msg|safe }}, statesForBallotWoExtern = {{ states_for_ballot_wo_extern }};

        $(document).ready(function () {
            function stateChanged() {
                var v = $(this).val();
                $("#id_message").val(info_msg[v] || "");

                if ($.inArray(+v, statesForBallotWoExtern) != -1)
                    $(".ballot-wo-extern").show();
                else
                    $(".ballot-wo-extern").hide();
            }
            $("#id_charter_state").on("click change keydown", stateChanged);
            $("#id_charter_state").trigger("click");
        });
    </script>
{% endblock %}